/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.gui.tbl;

import java.awt.Component;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

/**
 * @author Copy from here
 * http://www.coderanch.com/t/343795/GUI/java/Check-Box-JTable-header
 * @deprecated Not used
 */
@Deprecated
public class CheckBoxHeader extends JCheckBox
        implements TableCellRenderer, MouseListener {
  private static int i = 0;
  protected int column;
  protected boolean mousePressed = false;

  public CheckBoxHeader(ItemListener itemListener) {
    setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    this.addItemListener(itemListener);
    setSelected(true);
  }

  @Override
  public Component getTableCellRendererComponent(
          JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
    this.column = column;
    return this;
  }

  protected void setColumn(int column) {
    this.column = column;
  }

  public int getColumn() {
    return column;
  }

  protected void handleClickEvent(MouseEvent e) {
//    System.out.println("Click checkbox "+(i++)+" @"+this.hashCode());
    if (mousePressed) {
      mousePressed = false;
      JTableHeader header = (JTableHeader) (e.getSource());
      JTable clickedTable = header.getTable();
      int clickedColumn = header.columnAtPoint(e.getPoint());
      int modelColumn = clickedTable.convertColumnIndexToModel(clickedColumn);
      if (clickedColumn == this.column && modelColumn != -1) {
        doClick();
      }
    }
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    handleClickEvent(e);
//    ((JTableHeader) e.getSource()).repaint();
  }

  @Override
  public void mousePressed(MouseEvent e) {
    mousePressed = true;
  }

  @Override
  public void mouseReleased(MouseEvent e) {
  }

  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
  }
}
